package pl.jtb.ctl.tabory.dao.impl.mybatis.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import pl.jtb.ctl.tabory.dao.api.services.IUserService;
import pl.jtb.ctl.tabory.dao.data.vo.UserVO;
import pl.jtb.ctl.tabory.dao.impl.mybatis.base.BaseDaoService;

public class UserServiceImpl extends BaseDaoService<UserVO> implements IUserService {

    private final Logger logger = Logger.getLogger(UserRoleServiceImpl.class);

    private final static String MAPPER = "pl.jtb.ctl.taboty.UserMapper";
    private final static String SQL_SAVE_USER = ".saveUser";
    private final static String SQL_UPDATE_USER = ".updateUser";
    private final static String SQL_SELECT_ALL_USERS = ".selectAllUsers";
    private final static String SQL_SELECT_USER_BY_LOGON = ".selectUserByLogon";
    private final static String SQL_SELECT_USER_BY_ID = ".selectUserById";
    private final static String SQL_DELETE_USER = ".deleteUser";

    public UserServiceImpl(SqlSessionFactory sqlSessionFactory) {
        super(sqlSessionFactory);
    }

    @Override
    public UserVO getUser(String userLogon) throws Exception {
        logger.debug("Getting User with logon: " + userLogon);
        UserVO result = null;
        try {
            result = selectOne(MAPPER + SQL_SELECT_USER_BY_LOGON, userLogon);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

    @Override
    public UserVO getUser(Long userId) throws Exception {
        logger.debug("Getting User with id: " + userId);
        UserVO result = null;
        try {
            result = selectOne(MAPPER + SQL_SELECT_USER_BY_ID, userId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

    @Override
    public List<UserVO> getAllUsers() throws Exception {
        logger.debug("Getting all users");
        List<UserVO> result = null;
        try {
            result = selectList(MAPPER + SQL_SELECT_ALL_USERS, null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
        return result;
    }

    @Override
    public UserVO saveUpdateUser(final UserVO user) throws Exception {
        logger.debug("Save/Update User " + user);
        if (user.getId() == null) {
            saveUser(user);
        } else {
            updateUser(user);
        }
        return user;
    }

    private void saveUser(final UserVO user) throws Exception {
        logger.debug("Saving new User");
        try {
            create(MAPPER + SQL_SAVE_USER, user);
        } catch (Exception e) {
            user.setId(null);
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    private void updateUser(final UserVO user) throws Exception {
        logger.debug("Updating existing User");
        try {
            update(MAPPER + SQL_UPDATE_USER, user);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

    @Override
    public void deleteUser(UserVO user) throws Exception {
        logger.debug("Delete User: " + user);
        try {
            delete(MAPPER + SQL_DELETE_USER, user);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw e;
        }
    }

}
